package com.knife.blog.web.action;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.knife.blog.domain.BlogInfo;
import com.knife.blog.logic.BlogCircleService;
import com.knife.blog.logic.BlogDirService;
import com.knife.blog.logic.BlogMessageService;
import com.knife.blog.logic.BlogNewsDocService;
import com.knife.blog.logic.BlogReviewService;
import com.knife.blog.logic.BlogUserService;
import com.knife.blog.logic.PhotoService;
import com.knife.blog.logic.impl.BlogCircleServiceImpl;
import com.knife.blog.logic.impl.BlogCoreServiceImpl;
import com.knife.blog.logic.impl.BlogMessageServiceImpl;
import com.knife.blog.logic.impl.BlogReviewServiceImpl;
import com.knife.blog.logic.impl.BlogUserServiceImpl;
import com.knife.blog.logic.impl.PhotoServiceImpl;
import com.knife.util.CommUtil;
import com.knife.util.HtmlUtil;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;

public class BlogAction extends BaseCmdAction {

    private BlogDirService blogDirService = BlogCoreServiceImpl.getInstance();
    private BlogNewsDocService blogDocService = BlogCoreServiceImpl.getInstance();
    private PhotoService photoService = PhotoServiceImpl.getInstance();
    private BlogReviewService reviewService = BlogReviewServiceImpl.getInstance();
    private BlogMessageService messageService = BlogMessageServiceImpl.getInstance();
    private BlogUserService userService = BlogUserServiceImpl.getInstance();
    private BlogCircleService circleService = BlogCircleServiceImpl.getInstance();

    @Override
    public Object doBefore(WebForm form, Module module) {
        if (form.get("user") != null) {
            try {
                String username = URLDecoder.decode(URLEncoder.encode(
                        (String) form.get("user"), "ISO8859_1"), "utf-8");
                blog = userService.getUserBlog(username);
            } catch (Exception e) {
                System.out.println("错误的连接，blog不存在，或者已经被删除!" + e);
                e.printStackTrace();
            }
        }
        return super.doBefore(form, module);
    }

    public Page doInit(WebForm form, Module module) {
        Date d2 = new Date();
        Calendar ca = Calendar.getInstance();
        ca.setTime(d2);
        ca.roll(Calendar.DATE, -1);
        Date d1 = ca.getTime();
        List todayHeadList = blogDocService.getTopBlogNewsDoc(
                "displaytime desc", 2, 1);
        List eliteList = blogDocService.getTopBlogNewsDoc("displaytime desc",
                1, 5);
        List newsList = blogDocService.getTopBlogNewsDoc("displaytime desc", 0,
                15);
        form.addResult("todayHeadList", todayHeadList);
        form.addResult("eliteList", eliteList);
        form.addResult("list", newsList);
        form.addResult("hotReadList", blogDocService.getTopBlogNewsDoc(null,
                null, d1, d2, "readTimes desc", 0, 10));
        form.addResult("hotReviewList", blogDocService.getTopBlogNewsDoc(null,
                null, d1, d2, "replyNum desc", 0, 10));
        return module.findPage("home");
    }

    public Page doIndex(WebForm form, Module module) {
        int currentPage = CommUtil.null2Int(form.get("page"));
        int pageSize = CommUtil.null2Int(form.get("pageSize"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize < 1) {
            pageSize = 15;

        }
        IPageList pageList = blogDocService.queryBlogNewsDoc(
                blog.getUsername(), currentPage, pageSize);
        CommUtil.saveIPageList2WebForm(pageList, form);
        if (pageList != null) {
            form.addResult("pageSize", new Integer(pageSize));

        }
        return new Page("index" + blog.getUsername(), "/user/index.htm");
    }

    /*public Page doListDoc(WebForm form, Module module){
        int currentPage = CommUtil.null2Int(form.get("page"));
        int pageSize = CommUtil.null2Int(form.get("pageSize"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize < 1) {
            pageSize = 15;
        }
        String dir = (String) form.get("dir");
        blog=userService.getBlogInfo((String) form.get("cid"));
        if(blog==null){
        	form.addResult("msg", "blog不存在了");
        	return new Page("index" + blog.getUserName(), "/template/"
                    + blog.getTemplate() + "/indexSearch.htm");
        }
        IPageList pageList = blogDocService.queryBlogNewsDoc(
                blog.getUserName(), null, dir, null, null, null, currentPage, pageSize);
        form.addResult("linkService",com.knife.blog.logic.impl.FriendLinkServiceImpl.getInstance());
        form.addResult("HtmlUtil", HtmlUtil.getInstance());
        form.addResult("CommUtil", CommUtil.getInstance());
        form.addResult("userService", BlogUserServiceImpl.getInstance());
        form.addResult("messageService", BlogMessageServiceImpl.getInstance());
        form.addResult("reviewService", BlogReviewServiceImpl.getInstance());
        form.addResult("photoService", PhotoServiceImpl.getInstance());
        form.addResult("cocService", BlogCoreServiceImpl.getInstance());
        form.addResult("dirService", BlogCoreServiceImpl.getInstance());
        CommUtil.saveIPageList2WebForm(pageList, form);
        if (pageList != null) {
            form.addResult("pageSize", new Integer(pageSize));

        }
        return new Page("index" + blog.getUserName(), "/template/"
                + blog.getTemplate() + "/indexSearch.htm");
    }*/

    public Page doListDoc(WebForm form, Module module) {
        int currentPage = CommUtil.null2Int(form.get("page"));
        int pageSize = CommUtil.null2Int(form.get("pageSize"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize < 1) {
            pageSize = 15;
        }
        String dir = (String) form.get("dir");
        //BlogInfo blog=userService.getBlogInfo((String) form.get("cid"));
        IPageList pageList = blogDocService.queryBlogNewsDoc(
                blog.getUsername(), null, dir, null, null, null, currentPage, pageSize);
        CommUtil.saveIPageList2WebForm(pageList, form);
        //BlogCommUtil.showStaticPageHtml(fileName, page, pages)

        if (pageList != null) {
            form.addResult("pageSize", new Integer(pageSize));
        }
        //return new Page("index" + blog.getUserName(), "/blog/template/" + blog.getTemplate() + "/index.html");
        return new Page("index" + blog.getUsername(), "/user/indexSearch.htm");
    }

    public Page doPhoto(WebForm form, Module module) {
        int currentPage = CommUtil.null2Int(form.get("page"));
        int pageSize = CommUtil.null2Int(form.get("pageSize"));
        IActiveUser user = this.getCurrentUser(form);
        user.getUserName();
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize < 1) {
            pageSize = 15;

        }
        IPageList pageList = photoService.queryPhoto(blog.getUsername(),
                currentPage, pageSize);
        CommUtil.saveIPageList2WebForm(pageList, form);
        if (pageList != null) {
            form.addResult("pageSize", new Integer(pageSize));

        }
        return new Page("index" + blog.getUsername(), "/user/photo.htm");
    }

    public Page doCircle(WebForm form, Module module) {
        int currentPage = CommUtil.null2Int(form.get("page"));
        int pageSize = CommUtil.null2Int(form.get("pageSize"));
        if (currentPage < 1) {
            currentPage = 1;

        }
        if (pageSize < 1) {
            pageSize = 15;

        }
        IPageList pageList = circleService.queryBlogNewsDoc(null, blog.getUsername(), 0, null, null, "inputtime desc", currentPage,
                pageSize);
        CommUtil.saveIPageList2WebForm(pageList, form);
        if (pageList != null) {
            form.addResult("pageSize", new Integer(pageSize));

        }
        return new Page("circle" + blog.getUsername(), "/user/circle.htm");
    }

    public Page doMessage(WebForm form, Module module) {
        try {
            String username = URLDecoder.decode(URLEncoder.encode((String) form.get("username"), "ISO8859_1"), "utf-8");
            int currentPage = CommUtil.null2Int(form.get("page"));
            int pageSize = CommUtil.null2Int(form.get("pageSize"));
            if (currentPage < 1) {
                currentPage = 1;

            }
            if (pageSize < 1) {
                pageSize = 15;

            }
            blog = userService.getUserBlog(username);
            IPageList pageList = blogDocService.queryBlogNewsDoc(username,
                    currentPage, pageSize);
            CommUtil.saveIPageList2WebForm(pageList, form);
            if (pageList != null) {
                form.addResult("pageSize", new Integer(pageSize));

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return module.findPage("index");
    }

    public Page doReview(WebForm form, Module module) {
        try {
            String username = URLDecoder.decode(URLEncoder.encode((String) form.get("username"), "ISO8859_1"), "utf-8");
            int currentPage = CommUtil.null2Int(form.get("page"));
            int pageSize = CommUtil.null2Int(form.get("pageSize"));
            if (currentPage < 1) {
                currentPage = 1;

            }
            if (pageSize < 1) {
                pageSize = 15;

            }
            blog = userService.getUserBlog(username);
            IPageList pageList = blogDocService.queryBlogNewsDoc(username,
                    currentPage, pageSize);
            CommUtil.saveIPageList2WebForm(pageList, form);
            if (pageList != null) {
                form.addResult("pageSize", new Integer(pageSize));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return module.findPage("index");
    }

    public Page doRss(WebForm form, Module module) {
        try {
            String username = URLDecoder.decode(URLEncoder.encode((String) form.get("username"), "ISO8859_1"), "utf-8");
            int currentPage = CommUtil.null2Int(form.get("page"));
            int pageSize = CommUtil.null2Int(form.get("pageSize"));
            if (currentPage < 1) {
                currentPage = 1;

            }
            if (pageSize < 1) {
                pageSize = 15;

            }
            blog = userService.getUserBlog(username);
            IPageList pageList = blogDocService.queryBlogNewsDoc(username,
                    currentPage, pageSize);
            CommUtil.saveIPageList2WebForm(pageList, form);
            if (pageList != null) {
                form.addResult("pageSize", new Integer(pageSize));

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return module.findPage("index");
    }
}
